#include "MainMenus.h"
#include "Namespace.h"

#include "MainWindow.h"
#include "Managers/DataManager.h"

#include <QMessageBox>

MainMenus::MainMenus(QWidget *parent) : QMenuBar(parent) {
    fileMenu = new QMenu(this);
    editMenu = new QMenu(this);
    viewMenu = new QMenu(this);
    helpMenu = new QMenu(this);

    connect(fileMenu, &QMenu::triggered, this, &MainMenus::handleMenuTriggered);
    connect(editMenu, &QMenu::triggered, this, &MainMenus::handleMenuTriggered);
    connect(viewMenu, &QMenu::triggered, this, &MainMenus::handleMenuTriggered);
    connect(helpMenu, &QMenu::triggered, this, &MainMenus::handleMenuTriggered);

    // File Actions
    openFile = new QAction(fileMenu);
    openDir = new QAction(fileMenu);
    openRecent = new QAction(fileMenu);

    saveFile = new QAction(fileMenu);
    saveAll = new QAction(fileMenu);

    closeFile = new QAction(fileMenu);
    restoreFile = new QAction(fileMenu);

    exitApp = new QAction(fileMenu);

    // Help Actions
    aboutApp = new QAction(helpMenu);
    aboutQt = new QAction(helpMenu);

    // Add Actions
    fileMenu->addAction(openFile);
    fileMenu->addAction(openDir);
    fileMenu->addAction(openRecent);
    fileMenu->addSeparator();
    fileMenu->addAction(saveFile);
    fileMenu->addAction(saveAll);
    fileMenu->addSeparator();
    fileMenu->addAction(closeFile);
    fileMenu->addAction(restoreFile);
    fileMenu->addSeparator();
    fileMenu->addAction(exitApp);

    helpMenu->addAction(aboutApp);
    helpMenu->addAction(aboutQt);

    addMenu(fileMenu);
    addMenu(editMenu);
    addMenu(viewMenu);
    addMenu(helpMenu);

    Q_TR_NOTIFY(MainMenus)
}

MainMenus::~MainMenus() {
}

void MainMenus::reloadStrings() {
    fileMenu->setTitle(tr("File(&F)"));
    editMenu->setTitle(tr("Edit(&E)"));
    viewMenu->setTitle(tr("View(&V)"));
    helpMenu->setTitle(tr("Help(&H)"));

    openFile->setText(tr("Open File"));
    openDir->setText(tr("Open Folder"));
    openRecent->setText(tr("Open Recent"));

    saveFile->setText(tr("Save"));
    saveAll->setText(tr("Save All"));

    closeFile->setText(tr("Close File"));
    restoreFile->setText(tr("Restore File"));

    exitApp->setText(tr("Exit"));

    aboutApp->setText(tr("About QFrqEditor"));
    aboutQt->setText(tr("About Qt"));
}

void MainMenus::handleMenuTriggered(QAction *action) {
    QMenu *menu = qobject_cast<QMenu *>(sender());
    if (menu == fileMenu) {

    } else if (menu == editMenu) {

    } else if (menu == viewMenu) {

    } else if (menu == helpMenu) {
        if (action == aboutApp) {
            QMessageBox::information(
                qWindow, tr("About Qt"),
                tr("%1 ver%2.\nA subproject of QSynthesis.").arg(My::AppName, My::Version));
        } else if (action == aboutQt) {
            QMessageBox::aboutQt(qWindow, tr("About Qt"));
        }
    } else {
        Q_ASSERT(0);
    }
}
